package com.sujie;

import ch.qos.logback.core.encoder.ByteArrayUtil;
import io.netty.buffer.ByteBufUtil;
import lombok.extern.java.Log;

/**
 * @author: xinmi
 * @date: 2020/12/2
 * @description:
 */
@Log
public class JT809encrypt {

    private static int M1 = 10000020;
    private static int IA1 = 84585185;
    private static int IC1 = 30000000;

    public static void main(String[] argc){

        byte[] bresult = ByteArrayUtil.hexStringToByteArray("7e00200101000c0203010301000001ff0603020201000001ff03030104033e7f");

        byte[] afterencrypt = encrypt(0, bresult);

        log.info(ByteBufUtil.hexDump(afterencrypt));

        byte[] jiemi = encrypt(0, afterencrypt);

        log.info(ByteBufUtil.hexDump(jiemi));


    }

    public static byte[] encrypt(int key,byte [] data) {
        if(data == null) return null;

        byte[] array = data;//使用原对象，返回原对象

        //byte[] array = new byte[data.length]; //数组复制 返回新的对象
        //System.arraycopy(data, 0, array, 0, data.length);

        int idx=0;
        if(key==0){
            key=1;
        }
        int mkey = M1;
        if (0 == mkey )
        {
            mkey = 1;
        }
        while(idx<array.length){
            key = IA1 * ( key % mkey ) + IC1;
            array[idx]^=((key>>20)&0xFF);
            idx++;
        }
        return array;
    }
}
